package cn.edu.scujcc.customofsichuan;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import cn.edu.scujcc.customofsichuan.Application.UserApplication;


public class mine extends Fragment implements View.OnClickListener{


    Context context;
    private ImageView iv_userHead;
    private TextView tv_userName;
    private EditText et_userTalk;
    private UserApplication userInfo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context= getContext();
        userInfo = UserApplication.getInstance();
        //获取根视图
        View root=inflater.inflate(R.layout.fragment_mine, container, false);
        //根据名称找到相关组件
        View myPicture=root.findViewById(R.id.mine_MyPicture);
        View collection=root.findViewById(R.id.mine_Collection);
        View browsing_history=root.findViewById(R.id.mine_Browsing_History);
        View help=root.findViewById(R.id.mine_Help);
        View about=root.findViewById(R.id.mine_About);
        View setting=root.findViewById(R.id.mine_Setting);

        iv_userHead = root.findViewById(R.id.user_head);
        tv_userName = root.findViewById(R.id.user_name);
        et_userTalk = root.findViewById(R.id.user_talk);

        //添加监听事件
        myPicture.setOnClickListener(this);
        collection.setOnClickListener(this);
        browsing_history.setOnClickListener(this);
        help.setOnClickListener(this);
        about.setOnClickListener(this);
        setting.setOnClickListener(this);

        //如果当前应用程序Application全局变量为空则说明没有用户，便跳转到登录界面
        if (userInfo.userMap.isEmpty()==true){
            Intent intent=new Intent(context,login.class);
            intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TASK|intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);

        }else {
            //加载用户信息
            loadUserInfo();
        }
        return root;
    }

    private void loadUserInfo() {

        iv_userHead.setImageResource((Integer) userInfo.userMap.get("userHead"));
        tv_userName.setText(String.valueOf(userInfo.userMap.get("userName")));
        et_userTalk.setText(String.valueOf(userInfo.userMap.get("userTalk")));

    }


    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()){
            case R.id.mine_MyPicture:
                intent=new Intent(context,SecondPager_Mine_myPicture.class);
                startActivity(intent);
                break;
            case R.id.mine_Collection:
                intent=new Intent(context,SecondPager_Mine_Collection.class);
                startActivity(intent);
                break;
            case R.id.mine_Browsing_History:
                intent=new Intent(context,SecondPager_Mine_browsingHistory.class);
                startActivity(intent);
                break;
            case R.id.mine_Help:
                intent=new Intent(context,SecondPager_Mine_help.class);
                startActivity(intent);
                break;
            case R.id.mine_About:
                intent=new Intent(context,SecondPager_Mine_about.class);
                startActivity(intent);
                break;
            case R.id.mine_Setting:
                intent=new Intent(context,SecondPager_Mine_setting.class);
                startActivity(intent);
                break;
        }
    }

}